﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Collections.Generic
{
    public static class IListExtentions
    {
        public static void AddRange<T>(this IList<T> list, IEnumerable<T> itemsToAdd)
        {
            foreach (var item in itemsToAdd)
            {
                list.Add(item);
            }
        }

        public static IList<T> FluentAddRange<T>(this IList<T> list, IEnumerable<T> itemsToAdd)
        {
            foreach (var item in itemsToAdd)
            {
                list.Add(item);
            }
            return list;
        }

        public static IList<T> FluentAdd<T>(this IList<T> list, T item)
        {
            list.Add(item);
            return list;
        }

        public static T GetLast<T>(this IList<T> list)
            where T : class
        {
            if (list.IsNullOrEmpty())
                return null;
            return list[list.Count - 1];
        }

        /// <summary>
        /// 安全选择下标（超出范围返回null）
        /// </summary>
        public static T Select<T>(this IList<T> list, int index)
            where T : class
        {
            if (list.IsNullOrEmpty() || index >= list.Count)
                return null;
            return list[index];
        }
    }
}
